//
//  TitleContentTableViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/7/12.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

class TitleContentTableViewController: BaseTableViewController {
    
    var source: Array<Dictionary<String,String?>> = []
    
    
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        self.tableView.backgroundColor = UIColor.dftBack
        self.tableView.register(UINib(nibName: "ApprovalDetailCell", bundle: Bundle.main), forCellReuseIdentifier: "ApprovalDetailCell")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    
    
    // MARK: - Table view data source

    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        switch section {
        case 0:
            return 10
        default:
            return 0.0
        }
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        switch section {
        case 0:
            let view = UIView()
            
            view.backgroundColor = UIColor.dftBack
            
            return view
        default:
            return nil
        }
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return source.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = self.tableView.dequeueReusableCell(withIdentifier: "ApprovalDetailCell") as! ApprovalDetailCell
        
        cell.selectionStyle = .none
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if let cel = cell as? ApprovalDetailCell {
            if indexPath.row < source.count {
                cel.title = source[indexPath.row].keys.first
                
                if let content = source[indexPath.row].first?.value {
                    
                    cel.content = content
                    
                } else {
                    cel.content = ""
                }
            }
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if let content = source[indexPath.row].first?.value {

            if content.count > 0{
                
                let size = content.size(with: UIFont.systemFont(ofSize: 15), maxSize: CGSize.init(width: tableView.width - 32, height: 1000000))
                
                return 55 + size.height
                
            } else {
                
                return 65
                
            }
        } else {
            return 65
        }
    }

}
